<?php

error_reporting(E_ALL);

function bind($query, $parameters = array())
{
	$sql = str_replace(array('?', '\'?\'', '"?"'), '%s', $query, $num);
	if ($num !== count($parameters)) {
		exit('error bliad, raodenoba unda sheesabamebodes!');
	}
	foreach ($parameters as $par) {
		switch (gettype($par)) {
			case 'boolean':
			case 'integer':
			case 'double':
				$replace[] = $par;
			break;
			case 'NULL':
				$replace[] = 'NULL';
			break;
			case 'array':
			case 'object':
			case 'resource':
				exit('array, object and resource types are unsupported.');
			default:
				$replace[] = '\'' . $par . '\'';
			break;
		}
	}
	return vsprintf($sql, $replace);
}

$sql = "SELECT * FROM `rame` WHERE `name` = '?' AND `id` = ?;";
echo bind($sql, array('list1', 1));

/*
	private function _fetcher($query, $all = false, $cache = 0)
	{
		$this->execute($query);
		if ($loop === true ) {
			if ($cache === 0) {
				while ($r = mysql_fetch_array($this->resource, $this->fetchmode)) {
					$this->result[] = $r;
				}
			} else {
				if ($this->_cache('check', 'fetchall', $query, $cache)) {
					$this->result = $this->_cache('open', 'fetch', $query);
				} else {
					while ($r = mysql_fetch_array($this->resource, $this->fetchmode)) {
						$this->result[] = $r;
					}
					$this->_cache('save', 'fetchall', $query, 0, $this->result);
				}
			}
		} else {
			if (empty($cache)) {
				$this->result = mysql_fetch_array($this->resource, $this->fetchmode);
			} else {
				if ($this->_cache('check', 'fetch', $query, $cache)) {
					$this->result = $this->_cache('open', 'fetch', $query);
				} else {
					$this->result = mysql_fetch_array($this->resource, $this->fetchmode);
					$this->_cache('save', 'fetch', $query, 0, $this->result);
				}
			}
		}
		return empty($this->result) ? false : $this->result;
	}
*/

/*
	public function prepare($query, $arguments = array())
	{
		if (count($arguments) != substr_count($query, '?')) {
			trigger_error('?-ebis raodenoba unda emtxveodes array-shi mqonde mnishvnelobebis raodenobas!');
		}
		$offset = 0;	
		foreach ($arguments as $arg) {
//			if (is_numeric($arg) == false) {
//				$arg = '\'' . $this->escape($arg) . '\'';
//			}
			$offset = strpos($query, '?', $offset);
			$query = substr_replace($query, $arg, $offset, 1);
			$offset += strlen($arg);
		}
		return $query;
	}

function bind($query, $arguments = array())
{
	if (is_array($arguments)) {
		if (count($arguments) != substr_count($query, '?')) {
			exit('argumentebi ar sheesabameba mnishvnelobebis raodenobas!');	
		}
		$offset = 0;
		foreach ($arguments as $arg) {
			//if (!is_numeric($args[$i])) {
			//	$arg = "'". $this->escape($args[$i]) . "'";
			//}
			$offset = strpos($query, '?', $offset);		
			$query = substr_replace($query, $arg, $offset, 1);
			$offset += strlen((string)$arg);
		}	
		return $query;
	} else {
		exit('array unda gadaacodo meore argumentad shch!');
	}
}
*/

?>